<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>numpy.nditer &mdash; NumPy v1.18 Manual</title>
    
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" >
    <link rel="stylesheet" href="../../_static/graphviz.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '1.18.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../_static/language_data.js"></script>
    <script type="text/javascript" src="../../_static/js/copybutton.js"></script>
    <link rel="author" title="About these documents" href="../../about.html" >
    <link rel="index" title="Index" href="../../genindex.html" >
    <link rel="search" title="Search" href="../../search.html" >
    <link rel="top" title="NumPy v1.18 Manual" href="../../index.html" >
    <link rel="up" title="Indexing routines" href="../routines.indexing.html" >
    <link rel="next" title="numpy.nditer.close" href="numpy.nditer.close.html" >
    <link rel="prev" title="numpy.fill_diagonal" href="numpy.fill_diagonal.html" > 
  </head>
  <body>
<div class="container">
  <div class="top-scipy-org-logo-header" style="background-color: #a2bae8;">
    <a href="../../index.html">
      <img border=0 alt="NumPy" src="../../_static/numpy_logo.png"></a>
    </div>
  </div>
</div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
        <li class="active"><a href="https://numpy.org/">NumPy.org</a></li>
        <li class="active"><a href="https://numpy.org/doc">Docs</a></li>
        
        <li class="active"><a href="../../index.html">NumPy v1.18 Manual</a></li>
        

          <li class="active"><a href="../index.html" >NumPy Reference</a></li>
          <li class="active"><a href="../routines.html" >Routines</a></li>
          <li class="active"><a href="../routines.indexing.html" accesskey="U">Indexing routines</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="numpy.nditer.close.html" title="numpy.nditer.close"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="numpy.fill_diagonal.html" title="numpy.fill_diagonal"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="numpy.fill_diagonal.html"
                        title="previous chapter">numpy.fill_diagonal</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="numpy.nditer.close.html"
                        title="next chapter">numpy.nditer.close</a></p>
<div id="searchbox" style="display: none" role="search">
  <h4>Quick search</h4>
    <div>
    <form class="search" action="../../search.html" method="get">
      <input type="text" style="width: inherit;" name="q" />
      <input type="submit" value="search" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="numpy-nditer">
<h1>numpy.nditer<a class="headerlink" href="#numpy-nditer" title="Permalink to this headline">¶</a></h1>
<dl class="class">
<dt id="numpy.nditer">
<em class="property">class </em><code class="sig-prename descclassname">numpy.</code><code class="sig-name descname">nditer</code><a class="reference external" href="https://github.com/numpy/numpy/blob/v1.18.1/numpy/__init__.py"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#numpy.nditer" title="Permalink to this definition">¶</a></dt>
<dd><p>Efficient multi-dimensional iterator object to iterate over arrays.
To get started using this object, see the
<a class="reference internal" href="../arrays.nditer.html#arrays-nditer"><span class="std std-ref">introductory guide to array iteration</span></a>.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>op</strong><span class="classifier">ndarray or sequence of array_like</span></dt><dd><p>The array(s) to iterate over.</p>
</dd>
<dt><strong>flags</strong><span class="classifier">sequence of str, optional</span></dt><dd><p>Flags to control the behavior of the iterator.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">buffered</span></code> enables buffering when required.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">c_index</span></code> causes a C-order index to be tracked.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">f_index</span></code> causes a Fortran-order index to be tracked.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">multi_index</span></code> causes a multi-index, or a tuple of indices
with one per iteration dimension, to be tracked.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">common_dtype</span></code> causes all the operands to be converted to
a common data type, with copying or buffering as necessary.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">copy_if_overlap</span></code> causes the iterator to determine if read
operands have overlap with write operands, and make temporary
copies as necessary to avoid overlap. False positives (needless
copying) are possible in some cases.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">delay_bufalloc</span></code> delays allocation of the buffers until
a reset() call is made. Allows <code class="docutils literal notranslate"><span class="pre">allocate</span></code> operands to
be initialized before their values are copied into the buffers.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">external_loop</span></code> causes the <code class="docutils literal notranslate"><span class="pre">values</span></code> given to be
one-dimensional arrays with multiple values instead of
zero-dimensional arrays.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">grow_inner</span></code> allows the <code class="docutils literal notranslate"><span class="pre">value</span></code> array sizes to be made
larger than the buffer size when both <code class="docutils literal notranslate"><span class="pre">buffered</span></code> and
<code class="docutils literal notranslate"><span class="pre">external_loop</span></code> is used.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">ranged</span></code> allows the iterator to be restricted to a sub-range
of the iterindex values.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">refs_ok</span></code> enables iteration of reference types, such as
object arrays.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">reduce_ok</span></code> enables iteration of <code class="docutils literal notranslate"><span class="pre">readwrite</span></code> operands
which are broadcasted, also known as reduction operands.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">zerosize_ok</span></code> allows <a class="reference internal" href="numpy.nditer.itersize.html#numpy.nditer.itersize" title="numpy.nditer.itersize"><code class="xref py py-obj docutils literal notranslate"><span class="pre">itersize</span></code></a> to be zero.</p></li>
</ul>
</dd>
<dt><strong>op_flags</strong><span class="classifier">list of list of str, optional</span></dt><dd><p>This is a list of flags for each operand. At minimum, one of
<code class="docutils literal notranslate"><span class="pre">readonly</span></code>, <code class="docutils literal notranslate"><span class="pre">readwrite</span></code>, or <code class="docutils literal notranslate"><span class="pre">writeonly</span></code> must be specified.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">readonly</span></code> indicates the operand will only be read from.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">readwrite</span></code> indicates the operand will be read from and written to.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">writeonly</span></code> indicates the operand will only be written to.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">no_broadcast</span></code> prevents the operand from being broadcasted.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">contig</span></code> forces the operand data to be contiguous.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">aligned</span></code> forces the operand data to be aligned.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">nbo</span></code> forces the operand data to be in native byte order.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">copy</span></code> allows a temporary read-only copy if required.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">updateifcopy</span></code> allows a temporary read-write copy if required.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">allocate</span></code> causes the array to be allocated if it is None
in the <code class="docutils literal notranslate"><span class="pre">op</span></code> parameter.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">no_subtype</span></code> prevents an <code class="docutils literal notranslate"><span class="pre">allocate</span></code> operand from using a subtype.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">arraymask</span></code> indicates that this operand is the mask to use
for selecting elements when writing to operands with the
‘writemasked’ flag set. The iterator does not enforce this,
but when writing from a buffer back to the array, it only
copies those elements indicated by this mask.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">writemasked</span></code> indicates that only elements where the chosen
<code class="docutils literal notranslate"><span class="pre">arraymask</span></code> operand is True will be written to.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">overlap_assume_elementwise</span></code> can be used to mark operands that are
accessed only in the iterator order, to allow less conservative
copying when <code class="docutils literal notranslate"><span class="pre">copy_if_overlap</span></code> is present.</p></li>
</ul>
</dd>
<dt><strong>op_dtypes</strong><span class="classifier">dtype or tuple of dtype(s), optional</span></dt><dd><p>The required data type(s) of the operands. If copying or buffering
is enabled, the data will be converted to/from their original types.</p>
</dd>
<dt><strong>order</strong><span class="classifier">{‘C’, ‘F’, ‘A’, ‘K’}, optional</span></dt><dd><p>Controls the iteration order. ‘C’ means C order, ‘F’ means
Fortran order, ‘A’ means ‘F’ order if all the arrays are Fortran
contiguous, ‘C’ order otherwise, and ‘K’ means as close to the
order the array elements appear in memory as possible. This also
affects the element memory order of <code class="docutils literal notranslate"><span class="pre">allocate</span></code> operands, as they
are allocated to be compatible with iteration order.
Default is ‘K’.</p>
</dd>
<dt><strong>casting</strong><span class="classifier">{‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional</span></dt><dd><p>Controls what kind of data casting may occur when making a copy
or buffering.  Setting this to ‘unsafe’ is not recommended,
as it can adversely affect accumulations.</p>
<ul class="simple">
<li><p>‘no’ means the data types should not be cast at all.</p></li>
<li><p>‘equiv’ means only byte-order changes are allowed.</p></li>
<li><p>‘safe’ means only casts which can preserve values are allowed.</p></li>
<li><p>‘same_kind’ means only safe casts or casts within a kind,
like float64 to float32, are allowed.</p></li>
<li><p>‘unsafe’ means any data conversions may be done.</p></li>
</ul>
</dd>
<dt><strong>op_axes</strong><span class="classifier">list of list of ints, optional</span></dt><dd><p>If provided, is a list of ints or None for each operands.
The list of axes for an operand is a mapping from the dimensions
of the iterator to the dimensions of the operand. A value of
-1 can be placed for entries, causing that dimension to be
treated as <a class="reference internal" href="../constants.html#numpy.newaxis" title="numpy.newaxis"><code class="xref py py-obj docutils literal notranslate"><span class="pre">newaxis</span></code></a>.</p>
</dd>
<dt><strong>itershape</strong><span class="classifier">tuple of ints, optional</span></dt><dd><p>The desired shape of the iterator. This allows <code class="docutils literal notranslate"><span class="pre">allocate</span></code> operands
with a dimension mapped by op_axes not corresponding to a dimension
of a different operand to get a value not equal to 1 for that
dimension.</p>
</dd>
<dt><strong>buffersize</strong><span class="classifier">int, optional</span></dt><dd><p>When buffering is enabled, controls the size of the temporary
buffers. Set to 0 for the default value.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Notes</p>
<p><a class="reference internal" href="#numpy.nditer" title="numpy.nditer"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nditer</span></code></a> supersedes <a class="reference internal" href="numpy.flatiter.html#numpy.flatiter" title="numpy.flatiter"><code class="xref py py-obj docutils literal notranslate"><span class="pre">flatiter</span></code></a>.  The iterator implementation behind
<a class="reference internal" href="#numpy.nditer" title="numpy.nditer"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nditer</span></code></a> is also exposed by the NumPy C API.</p>
<p>The Python exposure supplies two iteration interfaces, one which follows
the Python iterator protocol, and another which mirrors the C-style
do-while pattern.  The native Python approach is better in most cases, but
if you need the coordinates or index of an iterator, use the C-style pattern.</p>
<p class="rubric">Examples</p>
<p>Here is how we might write an <code class="docutils literal notranslate"><span class="pre">iter_add</span></code> function, using the
Python iterator protocol:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">iter_add_py</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">addop</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">add</span>
<span class="gp">... </span>    <span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">out</span><span class="p">],</span> <span class="p">[],</span>
<span class="gp">... </span>                <span class="p">[[</span><span class="s1">&#39;readonly&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;readonly&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;writeonly&#39;</span><span class="p">,</span><span class="s1">&#39;allocate&#39;</span><span class="p">]])</span>
<span class="gp">... </span>    <span class="k">with</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">for</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span>            <span class="n">addop</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">c</span><span class="p">)</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">it</span><span class="o">.</span><span class="n">operands</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
</pre></div>
</div>
<p>Here is the same function, but following the C-style pattern:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">iter_add</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="gp">... </span>   <span class="n">addop</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">add</span>
<span class="gp">... </span>   <span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">out</span><span class="p">],</span> <span class="p">[],</span>
<span class="gp">... </span>               <span class="p">[[</span><span class="s1">&#39;readonly&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;readonly&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;writeonly&#39;</span><span class="p">,</span><span class="s1">&#39;allocate&#39;</span><span class="p">]])</span>
<span class="gp">... </span>   <span class="k">with</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span>       <span class="k">while</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">finished</span><span class="p">:</span>
<span class="gp">... </span>           <span class="n">addop</span><span class="p">(</span><span class="n">it</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">it</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">out</span><span class="o">=</span><span class="n">it</span><span class="p">[</span><span class="mi">2</span><span class="p">])</span>
<span class="gp">... </span>           <span class="n">it</span><span class="o">.</span><span class="n">iternext</span><span class="p">()</span>
<span class="gp">... </span>       <span class="k">return</span> <span class="n">it</span><span class="o">.</span><span class="n">operands</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
</pre></div>
</div>
<p>Here is an example outer product function:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">outer_it</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">mulop</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">multiply</span>
<span class="gp">... </span>    <span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">([</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">out</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;external_loop&#39;</span><span class="p">],</span>
<span class="gp">... </span>            <span class="p">[[</span><span class="s1">&#39;readonly&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;readonly&#39;</span><span class="p">],</span> <span class="p">[</span><span class="s1">&#39;writeonly&#39;</span><span class="p">,</span> <span class="s1">&#39;allocate&#39;</span><span class="p">]],</span>
<span class="gp">... </span>            <span class="n">op_axes</span><span class="o">=</span><span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">ndim</span><span class="p">))</span> <span class="o">+</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">y</span><span class="o">.</span><span class="n">ndim</span><span class="p">,</span>
<span class="gp">... </span>                     <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="n">x</span><span class="o">.</span><span class="n">ndim</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">y</span><span class="o">.</span><span class="n">ndim</span><span class="p">)),</span>
<span class="gp">... </span>                     <span class="kc">None</span><span class="p">])</span>
<span class="gp">... </span>    <span class="k">with</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span>        <span class="k">for</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">)</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span>
<span class="gp">... </span>            <span class="n">mulop</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">out</span><span class="o">=</span><span class="n">c</span><span class="p">)</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="n">it</span><span class="o">.</span><span class="n">operands</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">outer_it</span><span class="p">(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">)</span>
<span class="go">array([[1, 2, 3],</span>
<span class="go">       [2, 4, 6]])</span>
</pre></div>
</div>
<p>Here is an example function which operates like a “lambda” ufunc:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">luf</span><span class="p">(</span><span class="n">lamdaexpr</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="gp">... </span>   <span class="sd">&#39;&#39;&#39;luf(lambdaexpr, op1, ..., opn, out=None, order=&#39;K&#39;, casting=&#39;safe&#39;, buffersize=0)&#39;&#39;&#39;</span>
<span class="gp">... </span>   <span class="n">nargs</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span>
<span class="gp">... </span>   <span class="n">op</span> <span class="o">=</span> <span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;out&#39;</span><span class="p">,</span><span class="kc">None</span><span class="p">),)</span> <span class="o">+</span> <span class="n">args</span>
<span class="gp">... </span>   <span class="n">it</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">op</span><span class="p">,</span> <span class="p">[</span><span class="s1">&#39;buffered&#39;</span><span class="p">,</span><span class="s1">&#39;external_loop&#39;</span><span class="p">],</span>
<span class="gp">... </span>           <span class="p">[[</span><span class="s1">&#39;writeonly&#39;</span><span class="p">,</span><span class="s1">&#39;allocate&#39;</span><span class="p">,</span><span class="s1">&#39;no_broadcast&#39;</span><span class="p">]]</span> <span class="o">+</span>
<span class="gp">... </span>                           <span class="p">[[</span><span class="s1">&#39;readonly&#39;</span><span class="p">,</span><span class="s1">&#39;nbo&#39;</span><span class="p">,</span><span class="s1">&#39;aligned&#39;</span><span class="p">]]</span><span class="o">*</span><span class="n">nargs</span><span class="p">,</span>
<span class="gp">... </span>           <span class="n">order</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;order&#39;</span><span class="p">,</span><span class="s1">&#39;K&#39;</span><span class="p">),</span>
<span class="gp">... </span>           <span class="n">casting</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;casting&#39;</span><span class="p">,</span><span class="s1">&#39;safe&#39;</span><span class="p">),</span>
<span class="gp">... </span>           <span class="n">buffersize</span><span class="o">=</span><span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;buffersize&#39;</span><span class="p">,</span><span class="mi">0</span><span class="p">))</span>
<span class="gp">... </span>   <span class="k">while</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">finished</span><span class="p">:</span>
<span class="gp">... </span>       <span class="n">it</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">lamdaexpr</span><span class="p">(</span><span class="o">*</span><span class="n">it</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>
<span class="gp">... </span>       <span class="n">it</span><span class="o">.</span><span class="n">iternext</span><span class="p">()</span>
<span class="gp">... </span>       <span class="k">return</span> <span class="n">it</span><span class="o">.</span><span class="n">operands</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">luf</span><span class="p">(</span><span class="k">lambda</span> <span class="n">i</span><span class="p">,</span><span class="n">j</span><span class="p">:</span><span class="n">i</span><span class="o">*</span><span class="n">i</span> <span class="o">+</span> <span class="n">j</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span>
<span class="go">array([  0.5,   1.5,   4.5,   9.5,  16.5])</span>
</pre></div>
</div>
<p>If operand flags <em class="xref py py-obj">“writeonly”</em> or <em class="xref py py-obj">“readwrite”</em> are used the
operands may be views into the original data with the
<em class="xref py py-obj">WRITEBACKIFCOPY</em> flag. In this case <a class="reference internal" href="#numpy.nditer" title="numpy.nditer"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nditer</span></code></a> must be used as a
context manager or the <a class="reference internal" href="numpy.nditer.close.html#numpy.nditer.close" title="numpy.nditer.close"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nditer.close</span></code></a> method must be called before
using the result. The temporary data will be written back to the
original data when the <code class="xref py py-obj docutils literal notranslate"><span class="pre">__exit__</span></code> function is called but not before:</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="s1">&#39;i4&#39;</span><span class="p">)[::</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="n">np</span><span class="o">.</span><span class="n">nditer</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="p">[],</span>
<span class="gp">... </span>       <span class="p">[[</span><span class="s1">&#39;writeonly&#39;</span><span class="p">,</span> <span class="s1">&#39;updateifcopy&#39;</span><span class="p">]],</span>
<span class="gp">... </span>       <span class="n">casting</span><span class="o">=</span><span class="s1">&#39;unsafe&#39;</span><span class="p">,</span>
<span class="gp">... </span>       <span class="n">op_dtypes</span><span class="o">=</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">dtype</span><span class="p">(</span><span class="s1">&#39;f4&#39;</span><span class="p">)])</span> <span class="k">as</span> <span class="n">i</span><span class="p">:</span>
<span class="gp">... </span>   <span class="n">x</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">operands</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
<span class="gp">... </span>   <span class="n">x</span><span class="p">[:]</span> <span class="o">=</span> <span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]</span>
<span class="gp">... </span>   <span class="c1"># a still unchanged here</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="p">,</span> <span class="n">x</span>
<span class="go">(array([-1, -2, -3], dtype=int32), array([-1., -2., -3.], dtype=float32))</span>
</pre></div>
</div>
<p>It is important to note that once the iterator is exited, dangling
references (like <em class="xref py py-obj">x</em> in the example) may or may not share data with
the original data <em class="xref py py-obj">a</em>. If writeback semantics were active, i.e. if
<em class="xref py py-obj">x.base.flags.writebackifcopy</em> is <em class="xref py py-obj">True</em>, then exiting the iterator
will sever the connection between <em class="xref py py-obj">x</em> and <em class="xref py py-obj">a</em>, writing to <em class="xref py py-obj">x</em> will
no longer write to <em class="xref py py-obj">a</em>. If writeback semantics are not active, then
<em class="xref py py-obj">x.data</em> will still point at some part of <em class="xref py py-obj">a.data</em>, and writing to
one will affect the other.</p>
<p>Context management and the <a class="reference internal" href="numpy.nditer.close.html#numpy.nditer.close" title="numpy.nditer.close"><code class="xref py py-obj docutils literal notranslate"><span class="pre">close</span></code></a> method appeared in version 1.15.0.</p>
<dl class="field-list simple">
<dt class="field-odd">Attributes</dt>
<dd class="field-odd"><dl class="simple">
<dt><strong>dtypes</strong><span class="classifier">tuple of dtype(s)</span></dt><dd><p>The data types of the values provided in <a class="reference internal" href="numpy.nditer.value.html#numpy.nditer.value" title="numpy.nditer.value"><code class="xref py py-obj docutils literal notranslate"><span class="pre">value</span></code></a>. This may be
different from the operand data types if buffering is enabled.
Valid only before the iterator is closed.</p>
</dd>
<dt><strong>finished</strong><span class="classifier">bool</span></dt><dd><p>Whether the iteration over the operands is finished or not.</p>
</dd>
<dt><strong>has_delayed_bufalloc</strong><span class="classifier">bool</span></dt><dd><p>If True, the iterator was created with the <code class="docutils literal notranslate"><span class="pre">delay_bufalloc</span></code> flag,
and no reset() function was called on it yet.</p>
</dd>
<dt><strong>has_index</strong><span class="classifier">bool</span></dt><dd><p>If True, the iterator was created with either the <code class="docutils literal notranslate"><span class="pre">c_index</span></code> or
the <code class="docutils literal notranslate"><span class="pre">f_index</span></code> flag, and the property <a class="reference internal" href="numpy.nditer.index.html#numpy.nditer.index" title="numpy.nditer.index"><code class="xref py py-obj docutils literal notranslate"><span class="pre">index</span></code></a> can be used to
retrieve it.</p>
</dd>
<dt><strong>has_multi_index</strong><span class="classifier">bool</span></dt><dd><p>If True, the iterator was created with the <code class="docutils literal notranslate"><span class="pre">multi_index</span></code> flag,
and the property <a class="reference internal" href="numpy.nditer.multi_index.html#numpy.nditer.multi_index" title="numpy.nditer.multi_index"><code class="xref py py-obj docutils literal notranslate"><span class="pre">multi_index</span></code></a> can be used to retrieve it.</p>
</dd>
<dt><strong>index</strong></dt><dd><p>When the <code class="docutils literal notranslate"><span class="pre">c_index</span></code> or <code class="docutils literal notranslate"><span class="pre">f_index</span></code> flag was used, this property
provides access to the index. Raises a ValueError if accessed
and <code class="docutils literal notranslate"><span class="pre">has_index</span></code> is False.</p>
</dd>
<dt><strong>iterationneedsapi</strong><span class="classifier">bool</span></dt><dd><p>Whether iteration requires access to the Python API, for example
if one of the operands is an object array.</p>
</dd>
<dt><strong>iterindex</strong><span class="classifier">int</span></dt><dd><p>An index which matches the order of iteration.</p>
</dd>
<dt><strong>itersize</strong><span class="classifier">int</span></dt><dd><p>Size of the iterator.</p>
</dd>
<dt><strong>itviews</strong></dt><dd><p>Structured view(s) of <a class="reference internal" href="numpy.nditer.operands.html#numpy.nditer.operands" title="numpy.nditer.operands"><code class="xref py py-obj docutils literal notranslate"><span class="pre">operands</span></code></a> in memory, matching the reordered
and optimized iterator access pattern. Valid only before the iterator
is closed.</p>
</dd>
<dt><strong>multi_index</strong></dt><dd><p>When the <code class="docutils literal notranslate"><span class="pre">multi_index</span></code> flag was used, this property
provides access to the index. Raises a ValueError if accessed
accessed and <code class="docutils literal notranslate"><span class="pre">has_multi_index</span></code> is False.</p>
</dd>
<dt><strong>ndim</strong><span class="classifier">int</span></dt><dd><p>The dimensions of the iterator.</p>
</dd>
<dt><strong>nop</strong><span class="classifier">int</span></dt><dd><p>The number of iterator operands.</p>
</dd>
<dt><a class="reference internal" href="numpy.nditer.operands.html#numpy.nditer.operands" title="numpy.nditer.operands"><code class="xref py py-obj docutils literal notranslate"><span class="pre">operands</span></code></a><span class="classifier">tuple of operand(s)</span></dt><dd><p>operands[<em class="xref py py-obj">Slice</em>]</p>
</dd>
<dt><strong>shape</strong><span class="classifier">tuple of ints</span></dt><dd><p>Shape tuple, the shape of the iterator.</p>
</dd>
<dt><strong>value</strong></dt><dd><p>Value of <code class="docutils literal notranslate"><span class="pre">operands</span></code> at current iteration. Normally, this is a
tuple of array scalars, but if the flag <code class="docutils literal notranslate"><span class="pre">external_loop</span></code> is used,
it is a tuple of one dimensional arrays.</p>
</dd>
</dl>
</dd>
</dl>
<p class="rubric">Methods</p>
<table class="longtable docutils align-default">
<colgroup>
<col style="width: 10%" />
<col style="width: 90%" />
</colgroup>
<tbody>
<tr class="row-odd"><td><p><a class="reference internal" href="numpy.nditer.close.html#numpy.nditer.close" title="numpy.nditer.close"><code class="xref py py-obj docutils literal notranslate"><span class="pre">close</span></code></a>()</p></td>
<td><p>Resolve all writeback semantics in writeable operands.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="numpy.nditer.copy.html#numpy.nditer.copy" title="numpy.nditer.copy"><code class="xref py py-obj docutils literal notranslate"><span class="pre">copy</span></code></a>()</p></td>
<td><p>Get a copy of the iterator in its current state.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="numpy.nditer.debug_print.html#numpy.nditer.debug_print" title="numpy.nditer.debug_print"><code class="xref py py-obj docutils literal notranslate"><span class="pre">debug_print</span></code></a>()</p></td>
<td><p>Print the current state of the <a class="reference internal" href="#numpy.nditer" title="numpy.nditer"><code class="xref py py-obj docutils literal notranslate"><span class="pre">nditer</span></code></a> instance and debug info to stdout.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="numpy.nditer.enable_external_loop.html#numpy.nditer.enable_external_loop" title="numpy.nditer.enable_external_loop"><code class="xref py py-obj docutils literal notranslate"><span class="pre">enable_external_loop</span></code></a>()</p></td>
<td><p>When the “external_loop” was not used during construction, but is desired, this modifies the iterator to behave as if the flag was specified.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="numpy.nditer.iternext.html#numpy.nditer.iternext" title="numpy.nditer.iternext"><code class="xref py py-obj docutils literal notranslate"><span class="pre">iternext</span></code></a>()</p></td>
<td><p>Check whether iterations are left, and perform a single internal iteration without returning the result.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="numpy.nditer.remove_axis.html#numpy.nditer.remove_axis" title="numpy.nditer.remove_axis"><code class="xref py py-obj docutils literal notranslate"><span class="pre">remove_axis</span></code></a>(i)</p></td>
<td><p>Removes axis <em class="xref py py-obj">i</em> from the iterator.</p></td>
</tr>
<tr class="row-odd"><td><p><a class="reference internal" href="numpy.nditer.remove_multi_index.html#numpy.nditer.remove_multi_index" title="numpy.nditer.remove_multi_index"><code class="xref py py-obj docutils literal notranslate"><span class="pre">remove_multi_index</span></code></a>()</p></td>
<td><p>When the “multi_index” flag was specified, this removes it, allowing the internal iteration structure to be optimized further.</p></td>
</tr>
<tr class="row-even"><td><p><a class="reference internal" href="numpy.nditer.reset.html#numpy.nditer.reset" title="numpy.nditer.reset"><code class="xref py py-obj docutils literal notranslate"><span class="pre">reset</span></code></a>()</p></td>
<td><p>Reset the iterator to its initial state.</p></td>
</tr>
</tbody>
</table>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2019, The SciPy community.
      </li>
      <li>
      Last updated on Feb 20, 2020.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.4.2.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>